﻿<UserControl x:Class="GSF.PhasorProtocols.UI.UserControls.RealTimeMeasurementUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" d:DesignHeight="590" d:DesignWidth="850"
             xmlns:tsfBinding="clr-namespace:GSF.TimeSeries.UI.Binding;assembly=GSF.TimeSeries.UI"
             xmlns:tsf="clr-namespace:GSF.TimeSeries.UI;assembly=GSF.TimeSeries.UI.WPF"
             Background="Transparent">

    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/GSF.TimeSeries.UI.WPF;Component/Resources/CommonResource.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <StackPanel Orientation="Vertical">

        <StackPanel.Resources>

            <HierarchicalDataTemplate x:Key="MeasurementNodeTemplate">
                <Border BorderThickness="0,0,0,1" BorderBrush="#FFDDDDDD">                                       
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <StackPanel.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left"/>
                                <Setter Property="TextAlignment" Value="Left"/>
                                <Setter Property="VerticalAlignment" Value="Center"/>
                            </Style>
                        </StackPanel.Resources>

                        <TextBlock Text="{tsfBinding:Column Path=ID}" Width="150"/>
                        <TextBlock Text="{tsfBinding:Column Path=Description}" Width="350" TextWrapping="Wrap"/>
                        <TextBlock Text="{tsfBinding:Column Path=TimeTag}" FontWeight="Bold" Width="100" HorizontalAlignment="Center" TextAlignment="Center"/>
                        <TextBlock Text="{tsfBinding:Column Path=Value}" FontWeight="Bold" Width="90"
                                   Foreground="{tsfBinding:Column Path=Quality, Converter={StaticResource StringToStatusColorConverter}}" HorizontalAlignment="Center" TextAlignment="Center"/>
                        <TextBlock Text="{tsfBinding:Column Path=EngineeringUnit}" Width="60" HorizontalAlignment="Center" TextAlignment="Center"/>
                    </StackPanel>
                </Border>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="DeviceNodeTemplate" ItemsSource="{tsfBinding:Column Path=MeasurementList}" ItemTemplate="{StaticResource MeasurementNodeTemplate}">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">

                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left"/>
                            <Setter Property="TextAlignment" Value="Left"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </StackPanel.Resources>

                    <Ellipse Width="12" Height="12" VerticalAlignment="Center" Fill="{tsfBinding:Column Path=StatusColor, Converter={StaticResource StringToStatusColorConverter}}" Margin="2"/>
                    <TextBlock Text="{tsfBinding:Column Path=Acronym}" Width="175" FontWeight="Bold"/>
                    <TextBlock Text="{tsfBinding:Column Path=Name}" Width="300" FontWeight="Bold"/>
                    <TextBlock Text="{tsfBinding:Column Path=ProtocolName}" Width="135" FontWeight="Bold"/>
                    <TextBlock Text="{tsfBinding:Column Path=VendorDeviceName}" Width="100" FontWeight="Bold"/>
                    <Button Content="Edit" Tag="{tsfBinding:Column Path=Acronym}" Template="{StaticResource HyperlinkStyleButtonTemplate}" 
                            Width="50" Click="ButtonEdit_Click"  />
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate x:Key="RootNodeTemplate" ItemsSource="{tsfBinding:Column Path=DeviceList}" ItemTemplate="{StaticResource DeviceNodeTemplate}">

                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">

                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="HorizontalAlignment" Value="Left"/>
                            <Setter Property="TextAlignment" Value="Left"/>
                            <Setter Property="VerticalAlignment" Value="Center"/>
                        </Style>
                    </StackPanel.Resources>

                    <Ellipse Width="12" Height="12" VerticalAlignment="Center" Fill="{tsfBinding:Column Path=StatusColor, Converter={StaticResource StringToStatusColorConverter}}" Margin="2"/>
                    <TextBlock Text="{tsfBinding:Column Path=Acronym}" Width="175" FontWeight="Bold"/>
                    <TextBlock Text="{tsfBinding:Column Path=Name}" Width="550" FontWeight="Bold"/>
                    <Button Content="Edit" Template="{StaticResource HyperlinkStyleButtonTemplate}" Width="50" Click="ButtonEdit_Click"
                                Visibility="{tsfBinding:Column Path=ID, Converter={StaticResource ObjectToVisibilityConverter}}"/>
                </StackPanel>

            </HierarchicalDataTemplate>

            <!--<Style x:Key="ExpandedItemStyle" TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{tsfBinding:Column Path=DataContext.Expanded, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}}"/>
            </Style>-->
            <Style x:Key="ExpandedItemStyle" TargetType="TreeViewItem">
                <Setter Property="IsExpanded" Value="{tsfBinding:Column Path=Expanded}" />
            </Style>


        </StackPanel.Resources>

        <StackPanel Orientation="Vertical">
            
            <tsf:TsfPopup x:Name="PopupSettings" Topmost="False" Placement="Center">
                
                <Border CornerRadius="5" BorderThickness="1" Background="White" BorderBrush="Blue">

                    <Grid>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="2*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <TextBlock Text="Measurement Data Refresh Interval (in seconds)" Grid.Column="0" Grid.Row="0"/>
                        <TextBox x:Name="TextBoxRefreshInterval" Grid.Row="0" Grid.Column="1" Width="50"/>

                        <StackPanel Orientation="Horizontal" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Right">
                            <Button Content="Restore Default" Margin="2" Width="Auto" Click="ButtonRestore_Click"/>
                            <Button Content="Cancel" Margin="2" Click="ButtonCancel_Click"/>
                            <Button Content="Save" Margin="2" IsDefault="True" Click="ButtonSave_Click"/>
                        </StackPanel>

                    </Grid>

                </Border>
               
            </tsf:TsfPopup>

            <tsf:TsfPopup x:Name="ShowuserStatusDoc" Topmost="False" Placement="Center" >

                <Border CornerRadius="5" BorderThickness="2" Background="White" BorderBrush="Blue">

                    <Grid Margin="5" >
                        <Grid.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="HorizontalAlignment" Value="Left"/>
                                <Setter Property="TextAlignment" Value="Left"/>
                                <Setter Property="VerticalAlignment" Value="Center"/>
                                <Setter Property="Padding" Value="0"/>
                            </Style>

                            <Style TargetType="{x:Type CheckBox}">
                                <Setter Property="Width" Value="Auto"/>
                                <Setter Property="HorizontalAlignment" Value="Left"/>
                                <Setter Property="HorizontalContentAlignment" Value="Left"/>
                            </Style>

                        </Grid.Resources>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="45" MinWidth="1" MaxWidth="50" />
                            <ColumnDefinition Width="155" MinWidth="1" MaxWidth="165" />
                            <ColumnDefinition Width="450" MinWidth="1" MaxWidth="1005"/>
                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>


                        <TextBlock Text="Bit(s)" Grid.Row="0" Grid.Column="0" FontWeight="Bold" />
                        <TextBlock Text="Description" Grid.Row="0" Grid.Column="1" FontWeight="Bold"/>
                        <TextBlock Text="Comment" Grid.Row="0" Grid.Column="2" FontWeight="Bold"/>
                        <TextBlock Text="15" Grid.Row="1" Grid.Column="0"/>
                        <TextBlock Text="Data valid" Grid.Row="1" Grid.Column="1"/>
                        <TextBlock Text="0 when PMU data is valid, 1 when invalid or PMU is in test mode." Grid.Row="1" Grid.Column="2"/>
                        <TextBlock Text="14" Grid.Row="2" Grid.Column="0"/>
                        <TextBlock Text="PMU error" Grid.Row="2" Grid.Column="1"/>
                        <TextBlock Text="including configuration error, 0 when no error." Grid.Row="2" Grid.Column="2"/>
                        <TextBlock Text="13" Grid.Row="3" Grid.Column="0"/>
                        <TextBlock Text="PMU sync" Grid.Row="3" Grid.Column="1"/>
                        <TextBlock Text="0 when in sync." Grid.Row="3" Grid.Column="2"/>
                        <TextBlock Text="12" Grid.Row="4" Grid.Column="0"/>
                        <TextBlock Text="Data sorting" Grid.Row="4" Grid.Column="1"/>
                        <TextBlock Text="0 by time stamp, 1 by arrival." Grid.Row="4" Grid.Column="2"/>
                        <TextBlock Text="11" Grid.Row="5" Grid.Column="0"/>
                        <TextBlock Text="PMU trigger detected" Grid.Row="5" Grid.Column="1"/>
                        <TextBlock Text="0 when no trigger." Grid.Row="5" Grid.Column="2"/>
                        <TextBlock Text="10" Grid.Row="6" Grid.Column="0"/>
                        <TextBlock Text="Configuration changed" Grid.Row="6" Grid.Column="1"/>
                        <TextBlock Text="set to 1 for 1 min when configuration changed." Grid.Row="6" Grid.Column="2"/>
                        <TextBlock Text="09-06" Grid.Row="7" Grid.Column="0"/>
                        <TextBlock Text="Reserved for security" Grid.Row="7" Grid.Column="1"/>
                        <TextBlock Text="presently set to 0." Grid.Row="7" Grid.Column="2"/>
                        <TextBlock Text="05-04" Grid.Row="8" Grid.Column="0"/>
                        <TextBlock Text="Unlocked time" Grid.Row="8" Grid.Column="1"/>
                        <TextBlock Text="00 = sync locked, best quality." Grid.Row="8" Grid.Column="2"/>
                        <TextBlock Text="01 = Unlocked for 10 s " Grid.Row="9" Grid.Column="2"/>
                        <TextBlock Text="10 = Unlocked for 100 s" Grid.Row="10" Grid.Column="2"/>
                        <TextBlock Text="11 = Unlocked over 1000 s" Grid.Row="11" Grid.Column="2"/>
                        <TextBlock Text="03-00" Grid.Row="12" Grid.Column="0"/>
                        <TextBlock Text="Trigger reason" Grid.Row="12" Grid.Column="1"/>
                        <TextBlock Text="1111–1000:           Available for user definition" Grid.Row="12" Grid.Column="2"/>
                        <TextBlock Text="0111: Digital                         0110: Reserved" Grid.Row="13" Grid.Column="2"/>
                        <TextBlock Text="0101: df/dt high                   0100: Frequency high/low" Grid.Row="14" Grid.Column="2"/>
                        <TextBlock Text="0011: Phase-angle diff         0010: Magnitude high" Grid.Row="15" Grid.Column="2"/>
                        <TextBlock Text="0011: 0001: Magnitude low 0000: Manual" Grid.Row="16" Grid.Column="2"/>
                        <StackPanel Orientation="Horizontal" Grid.Row="18" Grid.Column="2" Grid.ColumnSpan="2" HorizontalAlignment="Right">
                            <Button Content="Cancel" Margin="0,0,5,0" Click="ButtonCancelShowuserStatusDoc_Click" IsCancel="True"/>
                        </StackPanel>
                    </Grid>

                </Border>

            </tsf:TsfPopup>

            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <Button x:Name="StatusFlagReferenceSettings" Content="StatusFlag Reference" Template="{StaticResource HyperlinkStyleButtonTemplate}" 
                    Width="Auto" Click="ButtonStatusFlagReference_Click"/>
                <Button x:Name="ButtonDisplaySettings" Content="Display Settings" Template="{StaticResource HyperlinkStyleButtonTemplate}" 
                    Width="Auto" Click="ButtonDisplaySettings_Click"/>
                <TextBlock Text="Refresh Interval: " Width="Auto"/>
                <TextBlock x:Name="TextBlockMeasurementRefreshInterval" Width="Auto"/>                
                <TextBlock Text="{Binding Path=DataContext.LastRefresh, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl, AncestorLevel=1}}"/>
            </StackPanel>

            <TreeView ItemsSource="{tsfBinding:Column Path=ItemsSource}" ItemTemplate="{StaticResource RootNodeTemplate}" ItemContainerStyle="{StaticResource ExpandedItemStyle}"
                      Height="610" VerticalAlignment="Top" HorizontalAlignment="Left"/>

        </StackPanel>

    </StackPanel>

</UserControl>
